﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using NaireSystem.Api;
using NaireSystem.Datas;
using TYBase;
using TYControls;

namespace NaireSystem.Views
{
    /// <summary>
    /// ViewLogin.xaml 的交互逻辑
    /// </summary>
    public partial class ViewLogin : TYWindow
    {
        public ViewLogin()
        {
            InitializeComponent();
            txtUserName.Focus();
        }

        private async void BtnClickLogin(object sender, RoutedEventArgs e)
        {
            if (txtUserName.Text.Null())
            {
                this.ShowMsg("请输入用户id");
                txtUserName.Focus();
                return;
            }
            if (txtPassword.Password.Null())
            {
                this.ShowMsg("请输入密码");
                txtPassword.Focus();
                return;
            }

            string userId = txtUserName.Text;
            string password = txtPassword.Password;
            var resData = await Run(() =>
            {
                return UserApi.Login(userId, password);
            });
            if (resData == null || resData.code != 0)
            {
                this.ShowMsg("登录异常!" + resData?.msg);
                return;
            }
            AppData.user = resData.data;
            this.Hide();
            new ViewUserManager().ShowDialog();
            Environment.Exit(0);
            //this.DialogResult = true;
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                BtnClickLogin(null, null);
            }
        }
    }
}
